package com.hxd.mall.widget

import android.content.Context
import android.graphics.Color
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.Interpolator
import androidx.viewpager.widget.ViewPager
import com.hxd.mall.widget.magicIndicator.HXLinePagerIndicator
import com.hxd.mall.widget.magicIndicator.HXPagerTitleView
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 * Create by yinzhengwei on 2019-11-26
 * @Function
 */
fun TabBind(
    magicIndicator: MagicIndicator, viewPager: ViewPager, pagerIndicators: List<String>,
    nColor: String? = "#ff333333",
    sColor: String? = "#ffE24840",
    startLineColor: Int? = -0x651db7c0,
    endLineColor: Int? = 0x4DE24840
) {
    magicIndicator.navigator = CommonNavigator(magicIndicator.context).apply {
        adapter = object : CommonNavigatorAdapter() {
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                return HXPagerTitleView(context).apply {
                    text = pagerIndicators[index]
                    setNormalSize(12f)
                    setSelectSize(14f)
                    normalColor = Color.parseColor(nColor)
                    selectedColor = Color.parseColor(sColor)
                    setOnClickListener {
                        viewPager.currentItem = index
                    }
                }
            }

            override fun getCount(): Int {
                return pagerIndicators.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                return HXLinePagerIndicator(context).apply {
                    startColor = startLineColor ?: -0x651db7c0
                    endColor = endLineColor ?: 0x4DE24840
                    mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    lineHeight = UIUtil.dip2px(context, 3.0).toFloat()
                    //lineWidth = UIUtil.dip2px(context, 50.0).toFloat()
                    roundRadius = UIUtil.dip2px(context, 1.5).toFloat()
                    startInterpolator = AccelerateInterpolator() as Interpolator?
                    endInterpolator = DecelerateInterpolator(2.0f)
                }
            }
        }
        isAdjustMode = true
    }
    ViewPagerHelper.bind(magicIndicator, viewPager)
}